var communityDAO = require('../models/communityDAO')

var communityController = {
    //增加评论
    addComment: function (req, res) {
        var comment = { commentContent: req.body.commentContent, userId: req.params.uId }
        communityDAO.addComment(comment, function (err, results) {
            if (err) {
                res.json({ code: 500, msg: '评论失败！' + err.message })

            } else {
                if (results.affectedRows == 0) {
                    res.json({ code: 200, msg: '评论失败！' })
                } else {
                    res.json({ code: 200, data: results, msg: '评论成功' })
                }
            }
        })
    },
    getList:function(req,res){
        communityDAO.getList(function(err,results){
            if (err) {
                res.json({ code: 500, msg: '获取评论失败！' + err.message })
            } else {
                console.log('111')
                res.json({ code: 200, data: results, msg: '获取评论成功' })
            }
        })
    },
    // 获取评论
    getCommentList: function (req, res) {
        var userId = req.query.userId
        console.log('个人评论Id：'+userId)
        communityDAO.getCommnetList(userId, function (err, results) {
            if (err) {
                res.json({ code: 500, msg: '获取评论失败！' + err.message })
            } else {
                res.json({ code: 200, data: results, msg: '获取评论成功' })
            }
        })
    },
    //删除评论
    delComment: function (req, res) {
          var uId=req.params.uId;        
        var commentId = req.query.commentId
        console.log("uId="+uId,"commentId="+commentId)
        //
        communityDAO.getCommentDetails(commentId,function(err,results){
            if(err){
                res.json({code:500,msg:'查询失败'})
            }else{
            //   if(results[0].userId==uId){
                    // 先删除回复表的内容
                    communityDAO.delReply(commentId, function (err, results) {
                        if (err) {
                            res.json({ code: 500, msg: '回复删除失败！' + err.message })
            
                        } else {
                            
                                communityDAO.delComment(commentId, function (err, results) {
                                    if (err) {
                                        res.json({ code: 500, msg: '评论删除失败！' + err.message })
            
                                    } else {
                                        if (results.affectedRows == 0) {
                                            res.json({ code: 200, msg: '评论删除失败！' })
                                        } else {
                                            res.json({ code: 200, data: results, msg: '评论删除成功' })
                                        }
            
                                    }
                                })
                            }
                        
                    })
                // }else{
                //     res.json({ code: 200, msg: '您没有权限删除该评论'  })
                // }
            }
        })
      

    },
    //新增回复
    addReply: function (req, res) {
        var reply = { replyContent: req.body.replyContent, userId: req.params.uId, commentId: req.body.commentId }
        communityDAO.addReply(reply, function (err, results) {
            if (err) {
                res.json({ code: 500, msg: '回复失败！' + err.message })

            } else {
                if (results.affectedRows == 0) {
                    res.json({ code: 200, msg: '回复失败！' })
                } else {
                    res.json({ code: 200, data: results, msg: '回复成功' })
                }

            }
        })
    },
    //获取回复
    getReply: function (req, res) {
        var commentId = req.params.commentId
        communityDAO.getReply(commentId, function (err, results) {
            if (err) {
                res.json({ code: 500, msg: '获取回复失败！' + err.message })
            } else {
                res.json({ code: 200, data: results, msg: '获取回复成功' })
            }
        })
    },
    //删除回复
    delReply: function (req, res) {
        var commentId = req.body.toCommentId
        communityDAO.delReply(commentId, function (err, results) {
            if (err) {
                res.json({ code: 500, msg: '删除失败！' + err.message })

            } else {
                if (results.affectedRows == 0) {
                    res.json({ code: 200, msg: '删除失败！' })
                } else {
                    res.json({ code: 200, data: results, msg: '删除成功' })
                }

            }
        })
    }
}
module.exports = communityController
